function canCheckIn(ety){
  if (!ety){
    alert("客单为空，不能入住");
    return false;
  }
  
  var src = $name("weordertasks/csourcename").value;
  if (src != "WalkIn" && ety.getPropertyByFieldName("cstatus") != "R"){
     alert("房号" + ety.getPropertyByFieldName("roomno") + "状态不为预订，不能入住!") ;
     return false;   
  }
  
  //可入住条件:入住客人详情已经填写，到达预定日期， 房间已经是空房且可用;
  //var x = ety.getProperty("cguestname") || "";
  var x = $id("grescguestname").value || "";
  if (!trim(x)){
     alert("房号" + ety.getPropertyByFieldName("roomno") +  "的客人姓名必须填写才能入住") ;
     return false;     
  }
  
  //x = ety.getProperty("cidtype") || "";
  x = $id("grescidtype").value || "";
  if (!trim(x)){
     alert("房号" + ety.getPropertyByFieldName("roomno") +  "的证件类型必须填写才能入住") ;
     return false;
  }
  
  //x = ety.getProperty("cidnumber") || "";
  x = $id("grescidnumber").value || "";
  if (!trim(x)){
     alert("房号" + ety.getPropertyByFieldName("roomno") +  "的证件号码必须填写才能入住") ;
     return false;
  }
  
  //TODO:校验身份证
  
  //判断是否可用
  x = ety.getProperty("croomstatus") || "";
  if (x == "O"){
     alert("房号" + ety.getPropertyByFieldName("roomno") + "已经在住，不能再次入住!") ;
     return false;
  }else if (x == "P"){
     alert("房号" + ety.getPropertyByFieldName("roomno") + "是待审核上线房间,不能入住!") ;
     return false;
  }else if (x == "N"){
     alert("房号" + ety.getPropertyByFieldName("roomno") + "是开发房,不能入住!") ;
     return false;
  }else if (x == "D"){
     alert("房号" + ety.getPropertyByFieldName("roomno") + "是无效房源,不能入住!") ;
     return false;
  }
  
  x = ety.getProperty("chkstatus") || "";
  if (x == "R"){
     alert("房间" + ety.getPropertyByFieldName("roomno") + "正在维修，不能入住!") ;
     return false;
  }else if (x == "S"){
     alert("房间" + ety.getPropertyByFieldName("roomno") + "已经停用,不能入住!") ;
     return false;
  }else if (x == "D"){
     alert("房间" + ety.getPropertyByFieldName("roomno") + "是业主自住房,不能入住!") ;
     return false;
  }else if (x == "B"){
     alert("房间" + ety.getPropertyByFieldName("roomno") + "正在维修,不能入住!") ;
     return false;
  }
  
  x = ety.getProperty("cclstatus") || "";
  /***if (x == "I"){
     alert("房间" + ety.getPropertyByFieldName("roomno") + "正在清洁检查中，不能入住!") ;
     return false;
  }else ***/if (x == "D"){
     alert("房间" + ety.getPropertyByFieldName("roomno") + "尚未清洁,不能入住!") ;
     return false;
  }
  
  x = stringToDate(ety.getProperty("arrorigdate"), 'yyyy-MM-dd').getTime() - 1 * 24 * 60 * 60 * 1000;
  if ((new Date()).getTime() < x){
     alert("未到入住时间，不能入住!");
     return false;
  }
  
  return true;
}

function checkIn(ety){
   if (!ety){
     return;
   }
   var isWalkin = $name("weordertasks/csourcename").value == "WalkIn";
   
   
  //先检查是否已经收取房费和押金，如果没有 
   var token = {
      walkinFlag:isWalkin,
      amount: getLineAmt(null, ety, 0, 0, null, "nrate"),
      deposit:Math.round(getLineAmt(null, ety, 0, 0, null, "nrate")/1 * 0.3* 10000) /10000 + ""
     };
     
   showModalCenter("OrderMgr/RateAndDeposit.jsp",
                   token,
                   function(args){
                      //将输入的房费和押金写入账单表
                      var x = {};
                      x.bills = [{
                            iitemid:1,
                            citemcode:"CASH",
                            citemdesc:'现金',
                            ncredit:args[0].rate.amount,
                            dbusinessdate:dateToString(toBusinessDate(new Date())),
                            cremarks:args[0].rate.remark,
                            cdescription:args[0].rate.remark
                        },{
                            iitemid:2,
                            citemcode:"DEPOSIT",
                            citemdesc:'押金',
                            ncredit:args[0].desposit.amount,
                            dbusinessdate:dateToString(toBusinessDate(new Date())),
                            cremarks:args[0].desposit.remark,
                            cdescription:args[0].desposit.remark
                        }];
                        
                        
                        //入住信息
                        x.gres = {
                           igresid:ety.getPropertyByFieldName("igresid"),
                           cguestname:$id("grescguestname").value,
                           csex:$id("grescsex").value,
                           dbirthday:$id("gresdbirthday").getValue(),
                           cidtype:$id("grescidtype").value,
                           cidnumber:$id("grescidnumber").value,
                           caddress:$id("grescaddress").value,
                           ctel:$id("gresctel").value,
                           cmobile:$id("grescmobile").value,
                           cemail:$id("grescemail").value,
                           cemail:$id("grescemail").value,
                           cdescription:$id("grescdescription").value,
                           darrivedbusinessdate:dateToString(toBusinessDate(new Date()))
                        };
                        
                      jrq(function(a){
                             var rst = eval("(" + a + ")");
                             //回写入住信息
                             for(var p in rst.gres){
                               ety.setPropertyByFieldName(p, rst.gres[p]);
                             }
                             
                             //在账单表上增加数据
                             var s = ety.getProperty("bills");
                             if (s){
                                for (var i = 0; i < rst.bills.length; i++){
                                  s.addEntity(Json2Entity(rst.bills[i]));
                                }
                             }
                             setCheckinInfoReadonly();
                             reloadBills(ety.getPropertyByFieldName("igresid"));
                             //$id("bills").freshFromEntity(eties);
                      }, 
                      "POST", 
                      JSON.stringify(x), 
                      "com.hms.OrderMgr.orderAjax.checkIn.biz.ext", 
                      false);
                   },
                   500,
                   220,
                   "收取房费和押金"
    );
      
}

function getGres(gresId){
  
  var ety = $id("gress").dataset.getAlltEntities();
  for(var i = 0; i < ety.length; i++){
    if (ety[i].getPropertyByFieldName("igresid") == gresId){
       return ety[i];
    }
  }
  return null;
  
}

//重新加载某行客单下的账单数据
function reloadBills(gresId){
  var ety = getGres(gresId);
  if (!ety){
     return;
  }
  var isActive = ety.getPropertyByFieldName("igresid") == $id("gress").getActiveEntity().getPropertyByFieldName("igresid");
  
  jrq(function(a){
        var rst = eval("(" + a + ")");
        ety.setPropertyByFieldName("bills", Json2Dataset(rst.bills));
        if (isActive){
           //刷新
           $id("bills").freshFromEntity(ety);
           calBalanceAmt();
        }
      }, 
      "POST", 
      "{'gresId':'" + gresId + "'}", 
      "com.hms.OrderMgr.orderAjax.queryGresTrans.biz.ext", 
      true);
}


//转账：将账单转到另外的客单
function transferAccount(srcId, targetId, bills, type){
  var x = {};
  
  x["srcId"] = srcId;
  x["targetId"] = targetId;
  x["bills"] = bills;
  
  jrq(function(a){
      }, 
      "POST", 
      JSON.stringify(x),
      type == "T"?"com.hms.OrderMgr.bills.transferBill.biz.ext":"com.hms.OrderMgr.bills.accountBill.biz.ext", 
      true);
}

